package org.linuxsogood.boot.controller;

import org.linuxsogood.boot.aop.AdminOnly;
import org.linuxsogood.boot.pojo.Permission;
import org.linuxsogood.boot.pojo.Person;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.UUID;

/**
 * @author honway.liu
 * @date 2016/11/6 下午1:55
 * @email gm100861@gmail.com
 * @blog http://linuxsogood.org
 */
@RestController
public class DemoController {

    @RequestMapping("/index")
    public String home() {
        return "index";
    }

    @RequestMapping(value="/upload", method= RequestMethod.POST)
    public @ResponseBody
    String handleFileUpload(MultipartHttpServletRequest request){
        List<MultipartFile> files = request.getFiles("file");
        for (int i =0; i< files.size(); ++i) {
            MultipartFile file = files.get(i);
            String originalFilename = file.getOriginalFilename();
            if (!file.isEmpty()) {
                try {
                    String substring = UUID.randomUUID().toString().substring(0, 8);
                    String ext = originalFilename.substring(originalFilename.lastIndexOf("."));
                    String fileName = substring + ext;
                    File classpath = ResourceUtils.getFile("classpath:application.yml");
                    String absolutePath = classpath.getParent();
                    byte[] bytes = file.getBytes();
                    BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(absolutePath + "/" + fileName)));
                    stream.write(bytes);
                    stream.close();
                } catch (Exception e) {
                    return "You failed to upload " + originalFilename + " => " + e.getMessage();
                }
            } else {
                return "You failed to upload " + originalFilename + " because the file was empty.";
            }
        }
        return "upload successful";
    }

    @RequestMapping(value="/uploadImg")
    public @ResponseBody String handleImgUpload(MultipartHttpServletRequest request, HttpServletResponse response){
        List<MultipartFile> files = request.getFiles("file");
        for (int i =0; i< files.size(); ++i) {
            MultipartFile file = files.get(i);
            String originalFilename = file.getOriginalFilename();
            if (!file.isEmpty()) {
                try {
                    BASE64Encoder encoder = new BASE64Encoder();
                    String encode = encoder.encode(file.getBytes());
                    String mime = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
                    String contentType = "data:image/" + mime + ";base64,";
                    return contentType + encode;
                } catch (Exception e) {
                    return "You failed to upload " + originalFilename + " => " + e.getMessage();
                }
            } else {
                return "You failed to upload " + originalFilename + " because the file was empty.";
            }
        }
        return "upload successful";
    }


    @RequestMapping("/check")
    @AdminOnly
    public String checkPermission() {
        return "check ok";
    }

    @RequestMapping("/user/person")
    public Person queryPermission(HttpServletResponse response) {
        //response.setHeader("Access-Control-Allow-Origin", "*");
        Person person = new Person();
        person.setAddress("Shanghai");
        person.setAge(29);
        person.setName("honway");
        return person;
    }
    @RequestMapping("/permission/person")
    public Permission queryPerson(HttpServletResponse response) {
        Permission permission = new Permission();
        permission.setId(1);
        permission.setPermissionCode("001");
        permission.setPermissionName("管理员");
        return permission;
    }
}
